package org.hsqldb;

import org.hsqldb.index.NodeBTree;
import org.hsqldb.index.NodeBTreeData;
import org.hsqldb.lib.java.JavaSystem;
import org.hsqldb.persist.PersistentStore;

// fredt@users 20020221 - patch 513005 by sqlbob@users (RMP)
// fredt@users 20020920 - patch 1.7.1 - refactoring to cut mamory footprint
// fredt@users 20021215 - doc 1.7.2 - javadoc comments

/**
 * Base class for a database row object implementing rows for
 * memory resident tables.<p>
 *
 * Subclass RowAVLDisk implements rows for CACHED and TEXT tables.<p>
 * New class derived from Hypersonic SQL code and enhanced in HSQLDB.<p>
 *
 * @author Thomas Mueller (Hypersonic SQL Group)
 * @nextAuthor Cao Yue (Tsinghua HyperSQL Group)
 * @version 2.0.1
 * @since Hypersonic SQL
 */
public class RowBTree extends Row {

    public NodeBTreeData nPrimaryNode;

    /**
     *  Default constructor used only in subclasses.
     */
    protected RowBTree(TableBase table, Object[] data) {
        super(table, data);
    }

    /**
     *  Constructor for MEMORY table Row. The result is a Row with Nodes that
     *  are not yet linked with other Nodes in the AVL indexes.
     */
    public RowBTree(TableBase table, Object[] data, int position,
                  PersistentStore store) {

        super(table, data);

        this.position = position;

        setNewNodes(store);
    }

    public void setNewNodes(PersistentStore store) {

        int indexCount = store.getAccessorKeys().length;

        nPrimaryNode = new NodeBTreeData(this);

        NodeBTreeData n = nPrimaryNode;

        for (int i = 1; i < indexCount; i++) {
        	
            n.nodeNextIndex = new NodeBTreeData(this);
            n       = (NodeBTreeData) n.nodeNextIndex;
        }
    }

    /**
     * Returns the Node for a given Index, using the ordinal position of the
     * Index within the Table Object.
     */
    public NodeBTreeData getNode(int index) {

    	NodeBTreeData n = nPrimaryNode;

        while (index-- > 0) {
            n = (NodeBTreeData)n.nodeNextIndex;
        }

        return n;
    }

    /**
     *  Returns the Node for the next Index on this database row, given the
     *  Node for any Index.
     */
    NodeBTreeData getNextNode(NodeBTreeData n) {

        if (n == null) {
            n = nPrimaryNode;
        } else {
            n = (NodeBTreeData)n.nodeNextIndex;
        }

        return n;
    }

    public NodeBTreeData insertNode(int index) {

    	NodeBTreeData backnode = getNode(index - 1);
    	NodeBTreeData newnode  = new NodeBTreeData(this);

        newnode.nodeNextIndex  = backnode.nodeNextIndex;
        backnode.nodeNextIndex = newnode;

        return newnode;
    }

    public void clearNonPrimaryNodes() {

    	NodeBTreeData n = (NodeBTreeData)nPrimaryNode.nodeNextIndex;

        while (n != null) {
            n.delete();

            n          = (NodeBTreeData)n.nodeNextIndex;
        }
    }

    public void delete(PersistentStore store) {

    	NodeBTreeData n = nPrimaryNode;

        while (n != null) {
            n.delete();

            n = (NodeBTreeData)n.nodeNextIndex;
        }
    }

    public void restore() {}

    /**
     * Helps GC, removing secondary node links.
     */
    public void destroy() {

        JavaSystem.memoryRecords++;

        clearNonPrimaryNodes();

        NodeBTreeData n = nPrimaryNode;

        while (n != null) {
        	NodeBTreeData last = n;

            n          = (NodeBTreeData)n.nodeNextIndex;
            last.nodeNextIndex = null;
        }
    }
}
